Imports pixolut.BizBlox
Imports NUnit.Framework

<TestFixture()> Public Class DOFactoryTests
    Public Sub New()
        MyBase.New()
    End Sub

    <TestFixtureSetUp()> Public Sub GlobalInit()
        'do nothing...
    End Sub

    <TestFixtureTearDown()> Public Sub GlobalDestruct()
        'do nothing...
    End Sub

    <SetUp()> Public Sub LocalInit()
        'do nothing...
    End Sub

    <TearDown()> Public Sub LocalDestruct()
        Setup.ClearDataSet()
    End Sub

    <Test()> Public Sub testCreateEmpty()
        Dim f As New DOFactory(GetType(Student))
        Dim sObj As Student = f.Create()
        Assert.AreEqual(sObj.GetType().ToString(), GetType(Student).ToString())
    End Sub

    <Test()> Public Sub testCreateGuid()
        Dim sObj As Student = Setup.CreateTestStudent()

        Dim uid As Guid = sObj.StudentID
        Dim f As New DOFactory(GetType(Student))
        Dim xObj As Student = f.Create(uid)
        xObj.Load()

        Assert.AreEqual(xObj.StudentID.ToString(), sObj.StudentID.ToString())
    End Sub


    <Test(), ExpectedException(GetType(DOException))> Public Sub testFailCreateGuid()
        Dim f As New DOFactory(GetType(Student))
        Dim sObj As Student = f.Create()
        'note - no save occurred.
        Dim xObj As Student = f.Create(sObj.StudentID)
        xObj.Load()
    End Sub

    <Test()> Public Sub testFailRetrieveGuid()
        Dim f As New DOFactory(GetType(Student))
        Dim sObj As Student = f.Create()
        'note - no save occurred.
        Dim xObj As Student = f.Retrieve(sObj.StudentID)
        Assert.IsNull(xObj)
    End Sub

    <Test()> Public Sub testFailRetrieveTO()
        Dim f As New DOFactory(GetType(Student))
        Dim sObj As Student = f.Create()
        Dim toObj As StudentTO = sObj.GetTO(GetType(StudentTO))
        'note - no save occurred.
        Dim xObj As Student = f.Retrieve(toObj)
        Assert.IsNull(xObj)
    End Sub

    <Test()> Public Sub testFailRetrieveNullTO()
        Dim f As New DOFactory(GetType(Student))
        Dim sObj As Student = f.Create()
        Dim toObj As New StudentTO
        'note - no save occurred.
        Dim xObj As Student = f.Retrieve(toObj)
        Assert.IsNull(xObj)
    End Sub

    <Test()> Public Sub testCreateTO()
        Dim sObj As Student = Setup.CreateTestStudent()
        Dim uid As Guid = sObj.StudentID
        Dim toObj As StudentTO = sObj.GetTO(GetType(StudentTO))

        Dim f As New DOFactory(GetType(Student))
        Dim xObj As Student = f.Create(toObj)
        Assert.IsFalse(xObj.StudentID.ToString().Equals(sObj.StudentID.ToString()))
        Assert.IsTrue(xObj.Name.Equals(sObj.Name))
    End Sub

    <Test()> Public Sub testRetrieveTO()
        Dim sObj As Student = Setup.CreateTestStudent()
        Dim uid As Guid = sObj.StudentID
        Dim toObj As StudentTO = sObj.GetTO(GetType(StudentTO))
        toObj.Name = "Something else"
        Dim f As New DOFactory(GetType(Student))
        Dim xObj As Student = f.Retrieve(toObj)
        Assert.IsTrue(xObj.StudentID.ToString().Equals(sObj.StudentID.ToString()))
        Assert.IsTrue(xObj.Name.Equals(sObj.Name))
        Assert.IsFalse(xObj.Name.Equals(toObj.Name))
    End Sub

    <Test()> Public Sub testRetrieveGuid()
        Dim sObj As Student = Setup.CreateTestStudent()
        Dim uid As Guid = sObj.StudentID

        Dim f As New DOFactory(GetType(Student))
        Dim xObj As Student = f.Retrieve(sObj.StudentID)

        'note: no load() is required...

        Assert.IsTrue(xObj.StudentID.ToString().Equals(sObj.StudentID.ToString()))
        Assert.IsTrue(xObj.Name.Equals(sObj.Name))
    End Sub

    <Test()> Public Sub testUpdateTO()
        Dim sObj As Student = Setup.CreateTestStudent()
        Dim uid As Guid = sObj.StudentID
        Dim toObj As StudentTO = sObj.GetTO(GetType(StudentTO))
        toObj.Name = "Something else"
        Dim f As New DOFactory(GetType(Student))
        Dim xObj As Student = f.Update(toObj)
        Assert.IsTrue(xObj.StudentID.ToString().Equals(toObj.StudentID.ToString()))
        Assert.IsFalse(xObj.Name.Equals(sObj.Name))
        Assert.IsTrue(xObj.Name.Equals(toObj.Name))
    End Sub

End Class